package com.wuming.caiwu.service.impl;


import com.wuming.caiwu.core.BusinessException;
import com.wuming.caiwu.core.WumingErrorCode;
import com.wuming.caiwu.dao.MemberMapper;
import com.wuming.caiwu.dao.QsgStoreMapper;
import com.wuming.caiwu.model.Member;
import com.wuming.caiwu.model.QsgStore;
import com.wuming.caiwu.service.QsgStoreService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
 * Created by hanfei on 16/5/7.
 */
@Service("storeService")
public class QsgStoreServiceImpl extends BaseServiceImpl implements QsgStoreService {

    @Autowired
    private QsgStoreMapper qsgStoreMapper;

    @Override
    public void modifyQsgStore(QsgStore member) throws BusinessException {
        qsgStoreMapper.updateByPrimaryKey(member);
    }

    @Override
    public QsgStore getQsgStoreById(Integer id) throws BusinessException {
        return qsgStoreMapper.selectByPrimaryKey(id);
    }

    @Override
    public void removeQsgStoreById(Integer id) throws BusinessException {
        qsgStoreMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<QsgStore> getQsgStoresByPage(String storeName, int pageIndex, int pageSize) throws BusinessException {
        Map<String,Object> params = new HashMap<>();
        if(StringUtils.isNotBlank(storeName)){
            params.put("storeName",storeName);
        }
        try{
            return (List<QsgStore>) this.getPageList(QsgStoreMapper.class, "selectByParams", params, pageIndex, pageSize);
        }catch(Exception e) {
            logger.error("查询店铺信息失败{}" + e.getMessage());
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_DATABASE_OPERATE_ERROR);
        }
    }

    @Override
    public int getTotal(String storeName) throws BusinessException {
        Map<String,Object> params = new HashMap<>();
        if(StringUtils.isNotBlank(storeName)){
            params.put("storeName",storeName);
        }
        try{
            return this.getTotal(QsgStoreMapper.class, "selectTotal", params);
        }catch (Exception e){
            logger.error("查询店铺信息总数失败.异常:{}", e.getMessage());
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_DATABASE_OPERATE_ERROR);
        }
    }

    @Override
    public List<QsgStore> getAll() throws BusinessException {
        return qsgStoreMapper.selectAll();
    }
}
